package cn.codexin.wechatofficial.controller;

import cn.codexin.wechatofficial.dto.StationDTO;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.CommonMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.StationMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.UnapprovedUserMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.UserMapper;
import cn.codexin.wechatofficial.model.dbo.*;
import cn.codexin.wechatofficial.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping(value = "user", produces = "application/json;charset=UTF-8")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    UnapprovedUserMapper unapprovedUserMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    StationMapper stationMapper;
    @Autowired
    CommonMapper commonMapper;

    @ResponseBody
    @GetMapping("interceptor")
    public Map<String, Object> user_info(@RequestParam(name = "code", required = true) String code,
                                         @RequestParam(name = "state", required = false) String state) {
        Map<String, Object> map = new HashMap<>();
        String userOpenId = userInfoService.getUserOpenId(code);
        Boolean checked = userInfoService.isChecked(userOpenId);
        log.info("interceptor get:" + userOpenId + " checked:" + checked);
        map.put("openid", userOpenId);
        //未注册和未审批都返回false
        if (checked) {
            map.put("access", true);
            map.put("msg", "访问成功");
        } else {
            map.put("access", false);
            map.put("msg", "被拦截");
        }
        return map;
    }

    @ResponseBody
    @GetMapping("getRegisterStation")
    public Map<String, Object> getRegisterStation(@RequestParam(value = "openid") String openid) {
        log.info("getRegisterStation-openid: "+openid);
        Map<String, Object> map = new HashMap<>();
        List<StationDTO> allStation = commonMapper.getAllStation();
        StationExample stationExample = new StationExample();
        stationExample.createCriteria().andOpenidEqualTo(openid);
        List<Station> stations = stationMapper.selectByExample(stationExample);
        List<Integer> stationList = stations.stream().map(Station::getStation).collect(Collectors.toList());
        List<StationDTO> RegisterStation = allStation.stream().filter(stationDTO -> !stationList.contains(stationDTO.getSubstationID())).collect(Collectors.toList());
        try {
            map.put("RegisterStation", RegisterStation);
            map.put("state", true);
            map.put("msg", "访问成功");
        } catch (RuntimeException e) {
            map.put("state", false);
            map.put("msg", "访问失败");
        }
        return map;
    }

    @ResponseBody
    @PostMapping("register")
    public Map<String, Object> register(@RequestParam(value = "openid") String openid,
                                        @RequestParam(value = "phone") String phone,
                                        @RequestParam(value = "name") String name,
                                        @RequestParam(value = "station") Integer station) {
        Map<String, Object> map = new HashMap<>();
        System.out.println("register get:" + openid + " " + phone + " " + name + " " + station);
        try {
            UnapprovedUser unapprovedUser = new UnapprovedUser();
            unapprovedUser.setOpenid(openid);
            unapprovedUser.setPhoneNumber(phone);
            unapprovedUser.setName(name);
            unapprovedUser.setStation(station);
            unapprovedUser.setGmtCreate(System.currentTimeMillis());
            unapprovedUser.setGmtModified(System.currentTimeMillis());
            unapprovedUserMapper.insertSelective(unapprovedUser);
            map.put("state", true);
            map.put("msg", "注册成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "更新失败,提示：" + e.getMessage());
        }
        return map;
    }

    @ResponseBody
    @GetMapping("switchDefaultStation")
    public Map<String, Object> stationByOpenid(@RequestParam(value = "openid", required = true) String openid,
                                               @RequestParam(value = "station", required = true) int station) {
        Map<String, Object> map = new HashMap<>();
        try {
            UserExample userExample = new UserExample();
            userExample.createCriteria().andOpenidEqualTo(openid);
            User user = new User();
            user.setDefaultSite(station);
            userMapper.updateByExampleSelective(user, userExample);
            map.put("state", true);
            map.put("msg", "更新成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "更新失败,提示：" + e.getMessage());
        }
        return map;
    }

    @ResponseBody
    @GetMapping("getStationByOpenid")
    public Map<String, Object> getStationByOpenid(@RequestParam(value = "openid", required = true) String openid) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(openid);
        try {
            StationExample stationExample = new StationExample();
            stationExample.createCriteria().andOpenidEqualTo(openid);
            List<Station> stations = stationMapper.selectByExample(stationExample);
            List<StationDTO> userStation = new ArrayList<>();
            stations.forEach(station -> {
                Integer stationID = station.getStation();
                String stationName = commonMapper.getStationNameByStationID(stationID);
                StationDTO temp = new StationDTO();
                temp.setSubstationID(stationID).setSubstationName(stationName);
                userStation.add(temp);
            });
            map.put("stations", userStation);
            map.put("state", true);
            map.put("msg", "访问成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "访问失败,提示：" + e.getMessage());
        }
        return map;
    }
}
